<?php
namespace app\manager\controller;

use think\Controller;

class User extends Controller
{
    /**
     * 验证用户是否处于登录状态
     */
    protected function _initialize()
    {
        $user = session('Muser');
        if(!$user){
            $this->redirect('login/index');
        }
    }


    public function index()
    {
        $userc = db('user')
            ->select();
        $this->assign('userc',$userc);
        return $this->fetch('index/num_list');
    }

    public function num_list_dele()
    {
        $param = input('post.');
        db('user')->where('u_id',$param['id'])->delete();
        echo "删除成功！";
    }

    public function num_edit()
    {
        $param = input('get.');
        $numcont = db('user')->where('u_id',$param['id'])->find();
        $this->assign('numcont',$numcont);
        return $this->fetch('index/num_edit');
    }

    public function num_edit_do()
    {
        $param = input('post.');

        if(empty($param['u_pass'])){
            db('user')
                ->where('u_id', $param['u_id'])
                ->update([
                    'u_phone'  => $param['u_phone'],
                    'u_nick' => $param['u_nick'],
                    'u_money' => $param['u_money'],
                    'u_dengji' => $param['u_dengji']
                ]);
        }
        if(!empty($param['u_pass'])){
            db('user')
                ->where('u_id', $param['u_id'])
                ->update([
                    'u_phone'  => $param['u_phone'],
                    'u_nick' => $param['u_nick'],
                    'u_money' => $param['u_money'],
                    'u_dengji' => $param['u_dengji'],
                    'u_pass' => md5($param['u_pass'])
                ]);
        }


        echo 200;
    }

    public function num_add()
    {
        return $this->fetch('index/num_add');
    }

    public function num_add_do()
    {
        $param = input('post.');

        if(empty($param['phone'])){
            return "手机号不能为空！";
        }

        if(empty($param['password'])){

            return "密码不能为空！";
        }
        $password=md5($param['password']);
        $crtime=date("Y-m-d H:i:s");
        $data = ['u_phone' => $param['phone'], 'u_pass' => $password, 'crtime' => $crtime];
        db('user')->insert($data);
        echo 200;
    }


}
